package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author LEO
 * @Date 2021年12月17日 11:06
 */
@Api(tags = "购物车服务接口")
@RestController
@RequestMapping("api/cart")
public class CartApiController {

    @Autowired
    private CartInfoService cartInfoService;


    // 提供接口查询数据给web-all使用
    @PostMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum")Integer skuNum,
                            HttpServletRequest request){
        // 缺少一个用户id   在网关中已经获取
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            //未登录:没有用户id 给一个临时的用户id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.addToCart(skuId,userId,skuNum);
        return Result.ok();
    }


    //查询购物车列表
    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request){
        // 缺少一个用户id   在网关中已经获取
        String userId = AuthContextHolder.getUserId(request);
        //未登录:没有用户id 给一个临时的用户id
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> carList = cartInfoService.getCarList(userId, userTempId);
        return Result.ok(carList);
    }

    //修改购物车勾选状态
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("isChecked") Integer isChecked,
                            HttpServletRequest request){
        // 获取用户id
      String userId   =   AuthContextHolder.getUserId(request);
      if (StringUtils.isEmpty(userId)){
          userId=AuthContextHolder.getUserTempId(request);
      }
        cartInfoService.checkCart(userId,isChecked,skuId);
        return Result.ok();
    }

    // 删除购物项
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId,
                             HttpServletRequest request){
        // 获取用户id
        String userId   =   AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId=AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCartInfo(userId,skuId);
        return Result.ok();
    }


    //根据用户Id 查询用户的收货地址列表！
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable(value = "userId") String userId) {
        return cartInfoService.getCartCheckedList(userId);
    }

    //根据userId 查询购物车最新价格
    @GetMapping("loadCartCache/{userId}")
    public Result loadCartCache(@PathVariable String userId){
        cartInfoService.loadCartCache(userId);
        return Result.ok();
    }
}
